package com.kai.mqtt.data;

import com.kai.mqtt.enums.ErrorCodeEnum;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author weijk
 * @since 2024/7/1
 */
@Data
@NoArgsConstructor
public class Result<T> {

    /**
     * 调用是否正常
     */
    private boolean success;

    /**
     * 返回对象
     */
    private T body;

    /**
     * 返回码，错误码
     */
    private int code;

    /**
     * 返回消息
     */
    private String msg;

    public Result(T body, int code, String msg, boolean success) {
        this.body = body;
        this.code = code;
        this.msg = msg;
        this.success = success;
    }

    public static <T> Result<T> fail() {
        return fail(ErrorCodeEnum.BASE_ERROR);
    }

    public static <T> Result<T> fail(ErrorCodeEnum errorCode) {
        return fail(errorCode.getCode(), errorCode.getMsg());
    }

    public static <T> Result<T> fail(int code, String message) {
        return new Result<>(null, code, message, false);
    }

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(data, ErrorCodeEnum.SUCCESS.getCode(), ErrorCodeEnum.SUCCESS.getMsg(), true);
    }
}
